package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCatalogQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyMongoRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMongoRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Strategy strategy) {

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        Strategy s = this.find(strategy.getId());
        strategy.setViewnum(s.getViewnum());
        strategy.setFavornum(s.getFavornum());
        strategy.setSharenum(s.getSharenum());
        strategy.setThumbsupnum(s.getThumbsupnum());
        strategy.setCreateTime(s.getCreateTime());
        repository.save(strategy);
    }

    @Override
    public Strategy find(String id) {
        Strategy strategy = repository.findById(id).orElse(null);

        return strategy;
    }

    @Override
    public List<Strategy> findAll() {
        List<Strategy> list = repository.findAll();
        return list;
    }

    @Override
    public Page query(StrategyQueryObject qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        long count = template.count(query, Strategy.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "sequence");
        query.with(page);
        List<Strategy> list = template.find(query, Strategy.class);
        return new PageImpl(list,page,count);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {

        //根据攻略查询出对应的攻略分类
        StrategyCatalog catalog = strategyCatalogService.find(strategy.getCatalogId());
        //根据攻略分类查询出目的地
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        //根据主题id查主题name,设置给攻略
        strategy.setThemeName(strategyThemeService.find(strategy.getThemeId()).getName());
        //设置catalogName
        strategy.setCatalogName(catalog.getName());
        //是否国外,先拿到destId,通过Id去找父目的地,如果是中国设置NO,外国设置YES
        List<Destination> toasts = destinationService.getToasts(strategy.getDestId());
        if(toasts != null && toasts.size() > 0){
            Destination dest = toasts.get(0);
            strategy.setIsabroad("中国".equals(dest.getName()) ? strategy.ABROAD_NO :strategy.ABROAD_YES);
        }
        if(!StringUtils.hasLength(strategy.getId())){
            strategy.setId(null);
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }else{
            this.update(strategy);
        }
    }

    @Override
    public List<Strategy> findByCatalogId(String id) {
        List<Strategy> list = repository.findByCatalogId(id);
        return list;
    }

    @Override
    public List<Strategy> viewnumTop3(String destId) {

        PageRequest of = PageRequest.of(0, 3);
        List<Strategy> strategies = repository.findByDestId(destId,of);
        return strategies;
    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByDestName(String destName) {
        List<Strategy> list = repository.findByDestName(destName);
        return list;
    }

}
